import { addPkorgRefer } from '../../../public/functions/addCondition';

export default class ButtonAction {
	constructor(comp) {
		this.comp = comp;
	}

	// 初始化页面按钮状态
	initPageButton = () => {
		const { props } = this.comp;
		const { button } = props;
		button.setButtonVisible({
			refresh: false,
			handentry: false,
			save: false,
			qryentry: false,
			psninfo: false,
			print: false
		});
		// button.setButtonDisabled({
		// });
	};

	// 按钮点击回调
	headerClick = (props, btnCode) => {
		const { action } = this.comp;
		// 查询
		if (btnCode === 'query') {
			this.showSearchModal();
		}
	};

	// 展示查询弹窗
	showSearchModal = () => {
		const { props } = this.comp;
		const { search } = props;
		search.openAdvSearch('query', true);
	};

	// 人力组织改变
	changeOrg = async (value) => {
		const { props } = this.comp;
		const { dispatch, button,emp} = props;
		if(typeof value == 'string'){
			console.log(value);
			dispatch({
				type: 'emp/update',
				payload: {
					orgValue: { refpk: '' },
					widget:true,
					widgetValue:value
				}
			});
			this.pubSub.publish('getWidgetTableData', {
				value: value,
				pageInfo: {
					pageIndex: 1,
					pageSize: 10,
					total: 0,
					totalPage: 1
				}, // 第一次请求的时候传输pageInfo信息，防止之前有过请求
			});
		}else{
			if (value) {
				button.setButtonDisabled({
					query: false
				});
				button.setButtonVisible({
					query: true
				});
				if(emp && emp.widget){
					console.log('change');
					await dispatch({
							type: 'emp/update',
							payload: {
								pageInfo: {
									pageIndex: 1,
									pageSize: 10,
									total: 0,
									totalPage: 1
								},
							}
					});
				}
			}
			dispatch({
				type: 'emp/update',
				payload: {
					orgValue: value,
					widget:false,
				}
			});
			if (emp.init) {
				this.pubSub.publish('getMainTableData', {
					pk_org: value.refpk
				});
				addPkorgRefer(props, { pk_org: value.refpk });
			}
		}
	};
	backToMain = () => {
		const { props } = this.comp;
		const { dispatch } = props;
		dispatch({
			type: 'emp/update',
			payload: {
				page: 'main',
				showOrgRefer: true
			}
		});
	};
}
